<?php

namespace App\Trip\Plane\Wrappers;

use Illuminate\Http\Request;

/**
 * 该期航班 包装器
 */
class ChangeFlightsResultWrapper
{
    private array   $flights;
    private Request $request;
    /**
     * @var int
     */
    private int $page;
    /**
     * @var int
     */
    private int $page_size;
    private int $total;

    public function __construct(array $flights, Request $request)
    {
        $this->flights   = $flights;
        $this->request   = $request;
        $this->page      = (int)$request->input('page', 1) ?: 1;
        $this->page_size = (int)$request->input('page_size', 15) ?: 1;
        $this->total     = collect($flights)->count();
    }

    public function getTotal()
    {
        return $this->total;
    }

    public function getFlights()
    {

        // 排序
        $sort_key   = $this->request->input('sort_key');
        $sort_value = $this->request->input('sort_value');
        $flights    = $this->flights;
        if ($sort_value && in_array($sort_key, ['depart_time', 'total_amount'])) {
            $sb      = $sort_value == 1 ? 'sortBy' : 'sortByDesc';
            $flights = collect($this->flights)
                ->$sb($sort_key);
        }

        return collect($flights)->forPage($this->page, $this->page_size)->values();
    }


    public function getPage(): int
    {
        return $this->page;
    }

    public function getPageSize(): int
    {
        return $this->page_size;
    }

}